import javax.swing.JOptionPane;
/**
 *   add a review by a reader. the review will be verified by the librarian before being posted
 */
public class AddReview 
{
	/** array of strings, used to send data in to the sever */
	public String[] query;
	/** array of strings, used to show the books available for review writing by the reader */
	public String[] res;
	
	/** initializes the variables */
	public AddReview(){}
	
	/** retrieves a list of books available for review writing by the reader */
	public void retreive()
	{
		/** holds the message from the sever */
		String temp;
		query=new String[3];
		query[0]=Main.gusers.id;
		query[1]="retrieve_book";
		query[2]=Main.gusers.id;
		Main.log.connection.accept("gReview");
		while(Main.log.flag==false);
		Main.log.flag=false;
		temp=ChatClient.getRs();
		res=temp.split("\\*");
		if(res[0].equals(""))
				res=null;
	}

	
	/** navigation button method, go back to the main screen */ 
	public void back()
	{
		Main.addReviewGui.panel.setVisible(false);
		Main.addReviewGui.setEnabled(false);
		if(Main.gusers.authorozations==1) Main.log.frame.setSize(450,320);
		else if(Main.gusers.authorozations==2) Main.log.frame.setSize(450,400);
		else if(Main.gusers.authorozations==3) Main.log.frame.setSize(450,515);
		Main.welcomeScreen.panel.setVisible(true);
		Main.welcomeScreen.panel.setEnabled(true);
	}
	/** add a review to the databse, the review will be verified by the librarian before posting */
	public void addreview()
	{
		/** flag, if true, the reader can send his review */
		boolean isValid=false;
		query=new String[5];
		query[0]=Main.gusers.id;
		query[1]="gAddReview";
		query[2]=Main.gusers.id;
		query[3]=(String)Main.addReviewGui.bookName.getSelectedItem();
		String temp=Main.addReviewGui.reviewText.getText();
		for(int i=0;i<temp.length();i++)
			if(Character.isLetter(temp.charAt(i)))
				isValid=true;
		if(isValid)
		{
			query[4]=temp;
			Main.log.connection.accept("gReview");
			while(Main.log.flag==false);
			Main.log.flag=false;
			if(ChatClient.getRs().equals("OK"))
				JOptionPane.showMessageDialog(null,"Review recieved in the system. waiting for approvment");
			else
				JOptionPane.showMessageDialog(null,"Server Error. Contact adminstrator","ERROR",JOptionPane.ERROR_MESSAGE);
		}
		else JOptionPane.showMessageDialog(null,"Your Review Must Contain Letters","ERROR",JOptionPane.ERROR_MESSAGE);
			
	}
	/**
	 * logs out of the system
	 */
	public void exit()
	{
		Main.addReviewGui.panel.setVisible(false);
		Main.addReviewGui.panel.setEnabled(false);
		Main.log.loginData=new String[3]; //GIL
		Main.log.loginData[0]=Main.gusers.id;
		Main.log.loginData[1]="logout";
		Main.log.loginData[2]=Main.gusers.id;
		Main.log.connection.accept("logout"); //GIL
		Main.log.flag=false;//GIL
		while(Main.log.flag==false);//GIL
		Main.log.flag=false;//GIL
		Main.log.user.setText("");
		Main.log.pass.setText("");
		Main.log.frame.setSize(650,550);
		Main.log.panel.setEnabled(true);
		Main.log.panel.setVisible(true);
	}
	
}
